<template>
  <div class="mod-config">
    <el-button type="primary" @click="goBack">返回上一页</el-button>
    <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
    <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;margin-top:20px;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column header-align="center" align="center" label="期数">
        <template slot-scope="scope">
          {{ scope.row.isAlive === 2 ? '--' : scope.row.chapterNo }}
        </template>
      </el-table-column>
      <el-table-column prop="chapterName" header-align="center" align="center" label="彩蛋名称"></el-table-column>
      <el-table-column prop="chapterVisit" header-align="center" align="center" label="配置播放量"></el-table-column>
      <el-table-column prop="chapterVisitCount" header-align="center" align="center" label="实际播放量"></el-table-column>
      <el-table-column prop="isAlive" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          {{ scope.row.isAlive === 1 ? '上架' : '' }}
          {{ scope.row.isAlive === 0 ? '下架' : '' }}
          {{ scope.row.isAlive === 2 ? '定时上架' : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="publishTime" header-align="center" align="center" label="上架时间">
        <template slot-scope="scope">
          {{ scope.row.publishTime || '--' }}
        </template>
      </el-table-column>
      <el-table-column prop="gmtCreated" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column prop="gmtUpdate" header-align="center" align="center" label="更新时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="detailView(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 查看 -->
    <view-detail v-if="viewDetail" ref="detail" @refreshDataList="getDataList"></view-detail>
  </div>
</template>

<script>
import AddOrUpdate from "./eggListAdd"
import ViewDetail from "./eggListView"
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      // 上架
      viewDetail: false,
      bookmanages: true,
    };
  },
  props: ["id"],
  watch: {
    bookId() {
      return this.id;
    }
  },
  mounted() {
    this.getDataList();
  },
  components: {
    AddOrUpdate,
    ViewDetail
  },
  methods: {
    // 获取数据列表 完毕
    getDataList() {
      this.$http({
        url: this.$http.adornUrl("/colorEgg/eggChapter/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          bookId: this.id
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data);
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    goBack() {
      this.$emit("goBack");
    },
    //查看
    detailView(id) {
      this.viewDetail = true;
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    //修改添加
    addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true;
        console.log(id)   //20
        let obj = {};
        if(id){
          obj.id = id;
        }
        obj.bookId = this.id;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(obj)
        })
      },
    //删除
    deleteHandle(id) {
      var ids = id? [id]: this.dataListSelections.map(item => {
            return item.id;
      });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/colorEgg/eggChapter/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  }
};
</script>
<style scoped>
.tableTitle {
  padding: 20px;
  font-size: 15px;
}
.el-row {
  font-size: 16px;
  margin: 15px 0;
}
.centerel {
  text-align: center;
  padding: 10px 20px;
  background: #f2f2f2;
  margin: 25px 0;
}
.line {
  display: flex;
  justify-content: center;
}
.line div {
  height: 100%;
  width: 1px;
  background: #bbb;
}
</style>
